#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
#
# Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
# Use is subject to license terms.
# Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
#

LIBBASENAME=	libc
LIBRARY=	$(LIBBASENAME:%=%.a)
VERS=		.1

# Note that we build libc_pic.a for the benefit of building
# ld.so.1, but we do not install it.  Only ld.so.1 needs it
# and it must be built in the same workspace as libc.
LIB_PIC= libc_pic.a
LIB_STATIC= libc_static.a

ROOTLIB=	$(ROOT)/usr/lib
ROOTFS_LIB=	$(ROOT)/lib
ROOTLIB64=	$(ROOTLIB)/$(MACH64)
ROOTFS_LIB64=	$(ROOTFS_LIB)/$(MACH64)

ROOTVARIANTDIR=	$(ROOTLIB)/libc
ROOTVARIANTDIR64=	$(ROOTLIB64)/libc

PROTOCOL_DIR=	$(ROOTHDRDIR)/rpcsvc
PROTOCOL_SRCDIR=	$(SRC)/head/rpcsvc
PROTOCOL_UTS_SRCDIR=	$(SRC)/uts/common/rpc

RPC_DERIVED_FILES=

GEN_DERIVED_FILES= \
	port/nis/gen/nis_clnt.h

PROTOCOL_FILES= \
	$(PROTOCOL_DIR)/daemon_utils.h	\
	$(PROTOCOL_DIR)/nis.x		\
	$(PROTOCOL_DIR)/nis.h		\
	$(PROTOCOL_DIR)/nis_object.x

PROTOCOL_FILES_UTS= \
	$(PROTOCOL_DIR)/key_prot.x

DERIVED_FILES= $(GEN_DERIVED_FILES) $(RPC_DERIVED_FILES)

#
# Make sure they get cleaned when necessary
#
CLEANFILES += $(DERIVED_FILES)

# include common library definitions
include ../Makefile.lib
include ../Makefile.rootfs

NSL_POFILE= libnsl.po
NSL_POFILES= generic.po _errlst.po
SOCKET_POFILE= libsocket.po

#
# on some architectures, we build multiple libc variants
#
VAR1 =		hwcap1
VAR2 =		hwcap2
VAR3 =		hwcap3

VARBLD_1=	$(MACH:sparc=sparc)
VARBLD=		$(VARBLD_1:i386=)

VARBLD64_1=	$(MACH:sparc=sparcv9)
VARBLD64=	$(VARBLD64_1:i386=)

VAR1_DYNLIB =	$(LIBRARY:%.a=%_$(VAR1).so$(VERS))
VAR1_BLDDIR =	$(MACH)_$(VAR1)/$(VARBLD)
VAR2_DYNLIB =	$(LIBRARY:%.a=%_$(VAR2).so$(VERS))
VAR2_BLDDIR =	$(MACH)_$(VAR2)/$(VARBLD)
VAR3_DYNLIB =	$(LIBRARY:%.a=%_$(VAR3).so$(VERS))
VAR3_BLDDIR =	$(MACH)_$(VAR3)/$(VARBLD)

VAR1_DYNLIB64 =	$(LIBRARY:%.a=%_$(VAR1).so$(VERS))
VAR1_BLDDIR64 =	$(MACH)_$(VAR1)/$(VARBLD64)

i386_VARIANTS=	$(VAR1) $(VAR2) $(VAR3)
sparc_VARIANTS=
sparcv9_VARIANTS=

i386_COMP=
sparc_COMP=	capabilities

MACH_COMP=	$($(MACH)_COMP)

i386_ETC=	$(MACH)/etc
sparc_ETC=

VARIANTS=	$($(MACH)_VARIANTS)
VARIANT_SUBDIRS	= $(VARIANTS:%=$(MACH)_%/$(VARBLD))

VARIANTS64=	$($(MACH64)_VARIANTS)
VARIANT_SUBDIRS64	= $(VARIANTS64:%=$(MACH)_%/$(VARBLD64))

SUBDIRS=
$(BUILD32)SUBDIRS += $(MACH_CAP) $(MACH) $(MACH_ETC) $(VARIANT_SUBDIRS)
$(BUILD64)SUBDIRS += $(MACH64) $(VARIANT_SUBDIRS64)

LIBS =		$(DYNLIB)

ROOTLIBS64=	$(ROOTLIB64)/$(DYNLIB)
MACHLIBS64=	$(MACH64)/$(DYNLIB)
VARIANTLIBS=	$(VARIANTS:%=$(MACH)_%/$(VARBLD)/$(LIBBASENAME)_%.so.1)
VARIANTLIBS64=	$(VARIANTS64:%=$(MACH)_%/$(VARBLD64)/$(LIBBASENAME)_%.so.1)
ROOTVARIANTLIBS= $(VARIANTS:%=$(ROOTVARIANTDIR)/$(LIBBASENAME)_%.so.1)
ROOTVARIANTLIBS64= $(VARIANTS64:%=$(ROOTVARIANTDIR64)/$(LIBBASENAME)_%.so.1)

# definitions for install_h target
BASEHDRS=	getxby_door.h
CHECKHDRS=	$(BASEHDRS:%.h=port/gen/%.check)
INCHDRS=	nss_priv.h libsocket_priv.h \
		md4.h md5.h sha1.h sha2.h skein.h

HDRS=		$(BASEHDRS) $(INCHDRS)

CLOBBERFILES += THIRDPARTYLICENSE extract-copyright

# install rules for install_h target
$(ROOTHDRDIR)/getxby_door.h: port/gen/getxby_door.h
	$(INS.file) port/gen/getxby_door.h

$(ROOTHDRDIR)/%: port/inc/%
	$(INS.file)

$(ROOTLIB)/$(DYNLIB)	:=	FILEMODE = 755
$(ROOTLIB64)/$(DYNLIB) :=	FILEMODE = 755
$(ROOTFS_LIBDIR)/$(DYNLIB)	:=	FILEMODE = 755
$(ROOTFS_LIBDIR64)/$(DYNLIB)	:=	FILEMODE = 755

$(ROOTVARIANTDIR)			:= FILEMODE= 755
$(ROOTVARIANTDIR)/$(VAR1_DYNLIB)	:= FILEMODE= 755
$(ROOTVARIANTDIR)/$(VAR2_DYNLIB)	:= FILEMODE= 755
$(ROOTVARIANTDIR)/$(VAR3_DYNLIB)	:= FILEMODE= 755

$(ROOTVARIANTDIR64)			:= FILEMODE= 755
$(ROOTVARIANTDIR64)/$(VAR1_DYNLIB64)	:= FILEMODE= 755

.KEEP_STATE:

all:	all_h lib32 $(BUILD64) .WAIT lib64 .WAIT etc THIRDPARTYLICENSE \
	$(PROTOCOL_DIR) $(DERIVED_FILES)

headers:	$(PROTOCOL_DIR) .WAIT $(PROTOCOL_FILES) $(PROTOCOL_FILES_UTS) \
		$(DERIVED_FILES)

THIRDPARTYLICENSE: extract-copyright
	$(RM) $@
	$(PERL) ./extract-copyright . > $@

etc:	$($(MACH)_ETC)

lib32:	$(MACH_COMP) $(MACHLIBS) $(MACH)/$(LIB_PIC) $(VARIANTLIBS)
	@if $(ELFDUMP) -r $(MACH)/$(DYNLIB) | $(GREP) -w environ ; then \
		$(ECHO) "Error: Invalid reference to environ" ; \
		$(ECHO) "Error: See comments in port/gen/env_data.c" ; \
		exit 1; \
	fi

lib64:	$(MACH_COMP) $(MACHLIBS64) \
		$(MACH64)/$(LIB_PIC) \
		$(VARIANTLIBS64)
	@if $(ELFDUMP) -r $(MACH64)/$(DYNLIB) | $(GREP) -w environ ; then \
		$(ECHO) "Error: Invalid reference to environ" ; \
		$(ECHO) "Error: See comments in port/gen/env_data.c" ; \
		exit 1; \
	fi

$(MACH)/assym.h $(MACH64)/assym.h:
	@cd $(@D); $(MAKE) assym.h

$(VARIANTLIBS): FRC
	@cd $(@D); pwd; VERSION='$(VERSION)' $(MAKE) $(@F)

$(VARIANTLIBS64): FRC
	@cd $(@D); pwd; VERSION='$(VERSION)' $(MAKE) $(@F)

$($(MACH)_ETC): FRC
	@cd $(@); pwd; $(MAKE) $(TARGET)

$($(MACH)_COMP): FRC
	@cd $(@); pwd; $(MAKE) $(TARGET)

install:	all			\
		etc			\
		inslib32

$(BUILD64)install: inslib64

inslib32:	$(ROOTFS_LIBS) $(ROOTFS_LINKS) $(ROOTVARIANTLIBS)

inslib64:	$(ROOTFS_LIBS64) $(ROOTFS_LINKS64) $(ROOTVARIANTLIBS64)

install_h: all_h $(ROOTHDRS)

all_h:	$(MACH)/assym.h $(MACH64)/assym.h

check:	$(CHECKHDRS)

$(ROOTVARIANTDIR): $(ROOTLIB)
	$(INS.dir)
$(ROOTVARIANTDIR)/%: $(ROOTVARIANTDIR) $(VAR1_BLDDIR)/%
	$(INS.file)
$(ROOTVARIANTDIR)/%: $(ROOTVARIANTDIR) $(VAR2_BLDDIR)/%
	$(INS.file)
$(ROOTVARIANTDIR)/%: $(ROOTVARIANTDIR) $(VAR3_BLDDIR)/%
	$(INS.file)

$(ROOTVARIANTDIR64): $(ROOTLIB64)
	$(INS.dir)
$(ROOTVARIANTDIR64)/%: $(ROOTVARIANTDIR64) $(VAR1_BLDDIR64)/%
	$(INS.file)

$(MACH)/$(LIB_PIC): FRC
	@cd $(MACH); pwd; VERSION='$(VERSION)' $(MAKE) $(LIB_PIC)
$(MACH64)/$(LIB_PIC): FRC
	@cd $(MACH64); pwd; VERSION='$(VERSION)' $(MAKE) $(LIB_PIC)
$(MACH64)/$(LIB_STATIC): FRC
	@cd $(MACH64); pwd; VERSION='$(VERSION)' $(MAKE) $(LIB_STATIC)

all	:=	TARGET= all
install	:=	TARGET= install
clean	:=	TARGET= clean
clobber	:=	TARGET= clobber

.PARALLEL:	$(SUBDIRS)

$(CAPDIR) \
$(SUBDIRS): FRC
	@cd $@; pwd; VERSION='$(VERSION)' $(MAKE) $(TARGET)

clean: $(SUBDIRS)
clobber: $(SUBDIRS) etc clobber_local
clobber_local:
	$(RM) $(CLOBBERFILES)

_msg:	$(MSGDOMAIN) catalog $(NSL_POFILE) $(SOCKET_POFILE)
	$(RM) $(MSGDOMAIN)/$(NSL_POFILE)
	$(RM) $(MSGDOMAIN)/$(SOCKET_POFILE)
	$(CP) $(NSL_POFILE) $(MSGDOMAIN)
	$(CP) $(SOCKET_POFILE) $(MSGDOMAIN)

$(SOCKET_POFILE):
	$(RM) messages.po
	$(XGETTEXT) `$(GREP) -l gettext port/inet/*.[ch] port/socket/*.[ch]`
	$(SED) -e '/^# msg/d' -e '/^domain/d' messages.po > $@
	$(RM) messages.po

$(NSL_POFILE): $(DERIVED_FILES) .WAIT $(NSL_POFILES)
	$(RM) $@
	$(CAT) $(NSL_POFILES) > $@

_errlst.po:
	$(RM) messages.po
	$(XGETTEXT) $(XGETFLAGS) port/nsl/_errlst.c
	$(SED) -e '/^# msg/d' -e '/^domain/d' messages.po > $@
	$(RM) messages.po

generic.po:
	$(RM) messages.po
	$(XGETTEXT) $(XGETFLAGS) `$(GREP) -l gettext */nsl*/*.[ch] */des/*.[ch] */dial/*.[ch] */ipsec/*.[ch] */key/*.[ch] */netdir/*.[ch] */netselect/*.[ch] */nss/*.[ch] */rpc/*.[ch] */saf/*.[ch] */yp/*.[ch] */nis/*/*.[ch]*`
	$(SED) -e '/^# msg/d' -e '/^domain/d' messages.po > $@
	$(RM) messages.po

catalog:
	XGETTEXT="$(XGETTEXT)" $(SHELL) ./makelibccatalog.sh $(MSGDOMAIN)

$(MSGDOMAIN):
	$(INS.dir)

include ../../Makefile.xref

# include MACH-specific library targets
include ../Makefile.mach
